﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LeadTeam.Exam.Interafce;
using LeadTeam.Exam.Entities;

namespace LeadTeam.Exam.DbContext
{
   public  class OrganizationsManipulate:IOrganizations
    {

        private DbContext dbContext = null;

        public OrganizationsManipulate()
        {
            dbContext = new DbContext();
        }

        public List<Entities.Organizations> GetList()
        {
            List<Organizations> list = dbContext.Set<Organizations>().ToList();
            return list;
        }

     

        public void DeleteEntity(string id)
        {
            int value = int.Parse(id);
            List<Organizations> list = dbContext.Set<Organizations>().ToList();
            List<int> itemsDeleted = new List<int>();
            itemsDeleted.Clear();
            itemsDeleted.Add(value);
            FindDeleted(value, itemsDeleted, list);

            foreach (int i in itemsDeleted)
            {
                var entity = dbContext.Organizations.Where(p => p.O_ID == i).First();
                dbContext.Organizations.Remove(entity);
                dbContext.SaveChanges();
            }
        }

        private void FindDeleted(int id, List<int> itemsDeleted, List<Organizations> list)
        {
            foreach (Organizations item in list)
            {
                if (item.O_PARENT_ID == id)
                {
                    itemsDeleted.Add(item.O_ID);
                    FindDeleted(item.O_ID, itemsDeleted, list);
                }
            }
        }

        public Entities.Organizations GetEntity(string id)
        {
            throw new NotImplementedException();
        }

        public void Update(string id, Entities.Organizations postEntity)
        {
            var set = dbContext.Set<Organizations>();
            set.Attach(postEntity);
            dbContext.Entry<Organizations>(postEntity).State = System.Data.EntityState.Modified;
            dbContext.SaveChanges();
        }

        public void UpdateAsParent(string id, string parentId)
        {
            int iId = int.Parse(id);

            var entity = dbContext.Organizations.FirstOrDefault(v => v.O_ID == iId);
            entity.O_PARENT_ID = int.Parse(parentId);
            dbContext.SaveChanges();
        }

        public void UpdateAsChild(string id,string parentId)
        {
            int iId = int.Parse(id);
            var list = dbContext.Organizations.Where(p => p.O_PARENT_ID ==iId);
            foreach (Organizations org in list)
            {
                org.O_PARENT_ID =int.Parse(parentId);
            }
            dbContext.SaveChanges();
        }


        public void CreateEntity_Child(Organizations postEntity)
        {
            dbContext.Set<Organizations>().Add(postEntity);
            dbContext.SaveChanges();
        }

        public void CreateEntity_Parent(Organizations postEntity,string id)
        {
            int value = int.Parse(id);
            var curEntity = dbContext.Organizations.Where(p => p.O_ID == value).FirstOrDefault();
            int parentId = curEntity.O_PARENT_ID;
            postEntity.O_PARENT_ID = parentId;
            Organizations addEntity = dbContext.Set<Organizations>().Add(postEntity);

            dbContext.SaveChanges();
            curEntity.O_PARENT_ID = addEntity.O_ID;
            dbContext.SaveChanges();
          
        }
    }
}
